package algo;

import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

public class TestMeili {
    private static class Account{
        int id;
        long wealth;

        public Account(int id, long wealth) {
            this.id = id;
            this.wealth = wealth;
        }

        public synchronized void balanceChange(long change){
            wealth+=change;
        }
    }

    public static void main(String[] args) {
        Random random = new Random();
        Account[] accts = new Account[8];
        for (int i = 0; i < 8; i++) {
            accts[i]= new Account(i,1000L);
        }
        for (int i = 0; i < 8; i++) {
            new Thread(()->{
                for (int j = 0; j < 10000; j++) {
                    int a1 = random.nextInt(8);
                    int a2 = random.nextInt(8);
                    accts[a1].balanceChange(5);
                    accts[a2].balanceChange(-5);
                }
            }).start();
        }
        long res = 0;
        for (int i = 0; i < 8; i++) {
            System.out.println(i+" wealth: "+accts[i].wealth);
            res+=accts[i].wealth;
        }
        System.out.println("total: "+res);
    }
}
